#ifndef __DRV_SA100_H__
#define __DRV_SA100_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include "sensor_filter.h"
#include "stdint.h"
#ifdef FREERTOS
#include "cmsis_os.h"
#endif
#include <stm32h7xx_hal_tim.h>

    typedef enum
    {
        First_Rrising,
        Falling,
        Second_Rising
    } Capture_Process_State;

#pragma pack(1)
    typedef struct
    {
        /* data */
        uint32_t CapVal[3];

        Capture_Process_State CapIndex;

        uint8_t CapFlag;
        uint32_t Period;
        uint32_t HighTime;
        float Duty;
#ifdef FREERTOS
        SemaphoreHandle_t xSA100GetPosSemap;
#endif
    } PWM_DUTY_Capture;
#pragma pack()

#pragma pack(1)

    typedef struct
    {
        PWM_DUTY_Capture SA100_Duty;
        FirstOrderKalmanFilter SA100_KF;
        float raw_angle;
        float filtered_angle;
        float initial_angle;
        float diff_angle;
    } Device_SA100_t;
#pragma pack()

    int SA100_Init(Device_SA100_t *SA100_dev);
    float SA100_GetPos(Device_SA100_t *SA100_dev);
    void SA100_Calibrate_n_Filter(Device_SA100_t *SA100_dev);
    void CaptureDutyCycle(Device_SA100_t *SA100_dev, TIM_HandleTypeDef *htim);

    // extern Device_SA100_t SA100_dev;

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __DRV_SA100_H__ */
